//
//  NSString+verify.m
//  kangzhipifuyisheng
//
//  Created by 唐开江 on 15/7/2.
//  Copyright (c) 2015年 KangZhi. All rights reserved.
//

#import "NSString+verify.h"

@implementation NSString (verify)



/**
 * 加密参数，规则：参数放入数组排序后MD5一次，标示md5一次，参数和标示拼接md5一次
 *
 *  @param paraArray  要加密的参数，key对应参数字段，value对应参数值，参数为空或者没参数就传@{@"":@""}，
 *
 *  @return 加密后的标示串
 */

+ (NSString *)encryptWithparaDict:(NSDictionary *)paraDict;
{

    
 /*
//1个参数：（传一个空）
NSString *str1 = [[NSString stringWithFormat:@"%@%@",[@"" md5String],[markSign md5String]] md5String];




  一个参数：
  NSString *str1 = [[NSString stringWithFormat:@"%@%@",[userDict[@"userId"] md5String],[markSign md5String]] md5String];
  NSString *str2 = [NSString stringWithFormat:@"http://www.duochong.com/地址名?userId=%@&verify=%@",userDict[@"userId"],str1];

  
//两个参数：(传一个userid + 一个参数 )

NSArray *array = [NSArray arrayWithObjects:@"userId",@"id", nil];
NSDictionary *dict = @{array[0]:userDict[@"userId"],array[1]:ID};
NSArray *sortedArray = [array sortedArrayUsingSelector:@selector(compare:)];
NSString *d1 =  dict[sortedArray[0]];
NSString *d2 =  dict[sortedArray[1]];
NSString *str = [NSString stringWithFormat:@"%@|%@",d1,d2];
NSString *str1 = [[NSString stringWithFormat:@"%@%@",[str md5String],[markSign md5String]] md5String];

  
  
//多个参数：(传一个userid + 其他参数 )

NSArray *array = [NSArray arrayWithObjects:@"userId",@"id",@"other", nil];
NSDictionary *dict = @{paraArray[0]:@"1",paraArray[1]:@"",paraArray[2]:@""};
NSArray *sortedArray = [paraArray sortedArrayUsingSelector:@selector(compare:)];
NSString *d1 =  dict[sortedArray[0]];
NSString *d2 =  dict[sortedArray[1]];
NSString *d3 =  dict[sortedArray[2]];


NSString *str = [NSString stringWithFormat:@"%@|%@|%@",d1,d2,d3];
NSString *str2 = [[NSString stringWithFormat:@"%@%@",[str md5String],[markSign md5String]] md5String];


    return  str2;
    
    
  */
    

    NSArray * allKeyArray = [paraDict allKeys];
    NSArray * allVauleArray = [paraDict allValues];
    
    //一个参数
    NSString * str1;
    if (allKeyArray.count==1) {
        
        NSString  * obj = allVauleArray.firstObject;

        str1 = [[NSString stringWithFormat:@"%@%@",[obj md5String],[markSign md5String]] md5String];
        
        
        //两个参数，多个参数
    }else if (allKeyArray.count>=2)
    {
        

        NSMutableDictionary * dict1 = [NSMutableDictionary dictionary];
        for (int i =0; i<allVauleArray.count; i++) {
              [dict1 setObject:allVauleArray[i] forKey:allKeyArray[i]];
        }
        
        NSArray *sortedArray = [allKeyArray sortedArrayUsingSelector:@selector(compare:)];
        
        NSMutableString * mutStr = [NSMutableString string];
        for (int i = 0;i<sortedArray.count;i++) {
            
            //参数之间添加“|”
            if (i<sortedArray.count-1) {
                
                NSString * d = dict1[sortedArray[i]];
                if ([d isEqual:[NSNull null]]) {
                    [mutStr appendFormat:@"|"];
                }else
                {
                    [mutStr appendFormat:@"%@|",d];
                }
                
                
            }else if (i==sortedArray.count-1)
            {
                //最后一个不加“|”
                NSString * d = dict1[sortedArray[i]];
                if (![d isEqual:[NSNull null]]) {
                    [mutStr appendFormat:@"%@",d];
                }
            }
            
        }
        
        
        
        str1 = [[NSString stringWithFormat:@"%@%@",[mutStr md5String],[markSign md5String]] md5String];
    }
    
    return  str1;
    


}



@end
